/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.Actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import pl.pg.misra.GUI.ConfigTopComponent;
import pl.pg.misra.rules.XMLRule;

@ActionID(category = "Tools",
id = "pl.pg.misra.analizerConfigurator")
@ActionRegistration(displayName = "#CTL_analizerConfigurator")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 25)
})
@Messages("CTL_analizerConfigurator=Configure analizer...")
public final class AnalizerConfigurator implements ActionListener {

    public void actionPerformed(ActionEvent e) { 
        try {
            FileWriter writ = new FileWriter("c:\\out.txt");
            BufferedWriter buf = new BufferedWriter(writ);
            
            buf.write(new java.io.File( "." ).getCanonicalPath());
            
            buf.close();
            
            writ.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }

        List<XMLRule> rules = new ArrayList<>();
        String clangPath = "";
        ConfigTopComponent configComponent = (ConfigTopComponent) WindowManager.getDefault().findTopComponent("ConfigTopComponent");
        
        try {
            rules = loadRules();
            clangPath = loadPath();
        } catch (SAXException | IOException | ParserConfigurationException ex) {
            Exceptions.printStackTrace(ex);
        }

        configComponent.loadRules(rules, clangPath);
        
        configComponent.open();
    }
    
    private List<XMLRule> loadRules() throws SAXException, IOException, ParserConfigurationException {
        List<XMLRule> result = new ArrayList<>();
        
        Node rule;
        Element ruleElement;

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse (new File("config.xml"));
        
        doc.getDocumentElement().normalize();
        
        NodeList listOfRules = doc.getElementsByTagName("rule");
        
        for (int index = 0; index < listOfRules.getLength(); index++){
            rule = listOfRules.item(index);
            
            if (rule.getNodeType() == Node.ELEMENT_NODE){
                ruleElement = (Element) rule;
                XMLRule xmlRule = new XMLRule();

                xmlRule.setId(Integer.parseInt(ruleElement.getAttribute("id")));
                xmlRule.setNumber(ruleElement.getElementsByTagName("number").item(0).getFirstChild().getNodeValue());
                xmlRule.setName(ruleElement.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
                xmlRule.setDescription(ruleElement.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
                xmlRule.setType(ruleElement.getElementsByTagName("type").item(0).getFirstChild().getNodeValue());
                xmlRule.setClassName(ruleElement.getElementsByTagName("className").item(0).getFirstChild().getNodeValue());
                xmlRule.setEnabled(Boolean.parseBoolean(ruleElement.getElementsByTagName("enabled").item(0).getFirstChild().getNodeValue()));
                
                result.add(xmlRule);
            }
        }
        
        return result;
    }

    private String loadPath() throws ParserConfigurationException, SAXException, IOException {
        String result = "";

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new File("config.xml"));
        
        doc.getDocumentElement().normalize();
        
        NodeList cygwinPath = doc.getElementsByTagName("clangPath");
        
        for (int index = 0; index < cygwinPath.getLength(); index++){
            Node cygwinPathNode = cygwinPath.item(index);
            
            if (cygwinPathNode.getNodeType() == Node.ELEMENT_NODE){
                Element cygwinPathElement = (Element) cygwinPathNode;

                result = cygwinPathElement.getFirstChild().getNodeValue();
            }
        }
        
        return result;
    }
}
